package flc.ast.activity;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;

import android.os.Bundle;
import android.view.View;

import com.blankj.utilcode.util.FileUtils;
import com.blankj.utilcode.util.PathUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import flc.ast.BaseAc;
import flc.ast.R;
import flc.ast.adapter.DrawRecordAdapter;
import flc.ast.databinding.ActivityDrawRecordBinding;
import flc.ast.utils.Content;
import io.reactivex.rxjava3.core.ObservableEmitter;
import stark.common.basic.event.EventStatProxy;
import stark.common.basic.utils.RxUtil;

public class DrawRecordActivity extends BaseAc<ActivityDrawRecordBinding> {

    private DrawRecordAdapter drawRecordAdapter;
    private List<String> listShow = new ArrayList<>();

    @Override
    protected int onCreate() {
        return R.layout.activity_draw_record;
    }

    @Override
    protected void initView() {
        EventStatProxy.getInstance().statEvent1(this, mDataBinding.event1Container);

        mDataBinding.ivDrawRecordBack.setOnClickListener(v -> onBackPressed());

        mDataBinding.rvDrawList.setLayoutManager(new GridLayoutManager(mContext, 2));
        drawRecordAdapter = new DrawRecordAdapter();
        mDataBinding.rvDrawList.setAdapter(drawRecordAdapter);
        drawRecordAdapter.addChildClickViewIds(R.id.ivDrawRecordItemDelete);
        drawRecordAdapter.setOnItemChildClickListener(this);
        drawRecordAdapter.setOnItemClickListener(this);
    }

    @Override
    protected void onItemClickCallback(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        DrawDetailActivity.imgPath = drawRecordAdapter.getItem(position);
        startActivity(DrawDetailActivity.class);
    }

    @Override
    public void onItemChildClick(@NonNull BaseQuickAdapter adapter, @NonNull View view, int position) {
        delete(drawRecordAdapter.getItem(position));
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void onResume() {
        super.onResume();
        getData();
    }

    private void getData() {
        listShow.clear();
        List<File> files = FileUtils.listFilesInDir(PathUtils.getFilesPathExternalFirst() + Content.myDrawRecord);
        if (files.size() > 0) {
            for (File file : files) {
                listShow.add(file.getPath());
            }
        }
        if (listShow.size() > 0) {
            drawRecordAdapter.setList(listShow);
            mDataBinding.rvDrawList.setVisibility(View.VISIBLE);
            mDataBinding.tvDrawNoData.setVisibility(View.GONE);
        } else {
            mDataBinding.rvDrawList.setVisibility(View.GONE);
            mDataBinding.tvDrawNoData.setVisibility(View.VISIBLE);
        }
    }

    //删除
    private void delete(String path) {
        RxUtil.create(new RxUtil.Callback<String>() {

            @Override
            public void doBackground(ObservableEmitter<String> emitter) {
                FileUtils.delete(path);
                emitter.onNext("");
            }

            @Override
            public void accept(String path) {
                dismissDialog();
                ToastUtils.showShort(R.string.delete_suc);
                getData();
            }
        });
    }

}